# encoding: utf-8
class Report::IncidenceReportController < ApplicationController

  def show
    @title_view ='Reporte de Incidencias'
    @heritages = CulturalHeritage::CulturalHeritage.all
    @users = User.all
  end

  def show_pdf
    @title_view ='Reporte de Incidencias'
    @heritage_name = params[:heritage_name]
    @user_name = params[:user_name]
    @incidence_state = params[:incidence_state]
    @cultural_heritages_incidences = CulturalHeritage::Incidence.all
    
    if ! @heritage_name.blank?
      if ! @user_name.blank?
        if ! @incidence_state.blank?
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.culturalHeritage = ? and cultural_heritage_incidences.user = ? and cultural_heritage_incidences.state = ?",@heritage_name,@user_name,@incidence_state])
        else
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.culturalHeritage = ? and cultural_heritage_incidences.user = ?",@heritage_name,@user_name])
        end
      else
        if ! @incidence_state.blank?
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.culturalHeritage = ? and cultural_heritage_incidences.state = ?",@heritage_name,@incidence_state])
        else
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.culturalHeritage = ?",@heritage_name])
        end
      end
    else
      if ! @user_name.blank?
        if ! @incidence_state.blank?
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.user = ? and cultural_heritage_incidences.state = ?",@user_name,@incidence_state])
        else
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.user = ?",@user_name])
        end
      else
        if ! @incidence_state.blank?
          @cultural_heritage_incidences = CulturalHeritage::Incidence.find(:all,:conditions=>["cultural_heritage_incidences.state = ?",@incidence_state.to_i])
        end
      end
    end


    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    pdf.text_box "Reporte de Incidencias por Patrimonio Cultural",
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20

    pdf.move_down 150

    arr = Array.new

    #Colocando headers
    arr[0] = Array.new
    arr[0][0] = "Código"
    arr[0][1] = "Usuario"
    arr[0][2] = "Patrimonio Cultural"
    arr[0][3] = "Descripción Breve"
    arr[0][4] = "Descripción Completa"
    arr[0][5] = "Estado"

    j = 1

    if !@cultural_heritage_incidences.nil?
      @cultural_heritage_incidences.each {
        |incidence|

        arr[j] = Array.new
        arr[j][0] = incidence.id
        arr[j][1] = incidence.obj_user.name + " " + incidence.obj_user.lastname
        arr[j][2] = incidence.obj_cultural_heritage.name
        arr[j][3] = incidence.briefDesc
        arr[j][4] = incidence.completeDesc
        arr[j][5] = incidence.str_state
        j = j + 1
      }
    end

    pdf.table arr, :column_widths => [60, 90, 90, 90 ,140, 70], :row_colors => ["F0F0F0", "FFFFFF"], :header => true do
      row(0).border_width = 2
      row(0).font_style = :bold
    end

    #Pie de pagina
    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end

    send_data pdf.render, :filename => "Reporte de Incidencias" << ".pdf", :type => "application/pdf"

  end

  def pdf_export

    incidence = CulturalHeritage::Incidence.find(params[:id])

    pdf = Prawn::Document.new

    pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :width => 80, :at => [0, 715]
    pdf.text_box "Sistema de información cultural pukara",
      :at => [100, 715],
      :width => 430
    pdf.text_box "Incidencia de Patrimonio Cultural N° " << params[:id],
      :at => [100, 690],
      :width => 430,
      :style => :bold,
      :size => 20
#    pdf.text_box params[:id],
#      :at => [100, 655],
#      :width => 430,
#      :style => :bold,
#      :size => 20

    if !(incidence.avatar.url.blank? || incidence.avatar_file_name.blank?)
      if ((incidence.avatar_file_name.include?('.jpg')) || (incidence.avatar_file_name.include?('.jpeg')) || (incidence.avatar_file_name.include?('.png')))
        pdf.image ("#{RAILS_ROOT}" << "/public" << incidence.avatar.url).split("?").at(0), :height => 200, :position => :center, :vposition => 130
      else
        pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :height => 200, :position => :center, :vposition => 130
      end
    else
      pdf.image "#{RAILS_ROOT}/public/images/pukara_logo.png", :height => 200, :position => :center, :vposition => 130
    end

    pdf.move_down 160

    pdf.text "<b>Código:</b> " << params[:id],
      :inline_format => true

    pdf.move_down 20

    if !incidence.user.blank?
      pdf.text "<b>Usuario:</b> " << incidence.obj_user.name + " " + incidence.obj_user.lastname,
        :inline_format => true

      pdf.move_down 20
    end

    pdf.text "<b>Patrimonio Cultural:</b> " << incidence.obj_cultural_heritage.name,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Lugar:</b> " << incidence.location,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>DNI Responsable:</b> " << incidence.dniResponsable,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Responsable:</b> " << incidence.fullNameResponsable,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Descripción Breve:</b> " << incidence.briefDesc,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Descripción Completa:</b> " << incidence.completeDesc,
      :inline_format => true

    pdf.move_down 20

    pdf.text "<b>Estado:</b> " << incidence.str_state,
      :inline_format => true



    pdf.repeat(:all, :dynamic => true) do
      pdf.draw_text Time.now.strftime("%d-%m-%Y").to_s, :at => [0, 0]
      pdf.draw_text pdf.page_number, :at => [530, 0]
    end


    send_data pdf.render, :filename => "Incidencia " << params[:id] << ".pdf", :type => "application/pdf"
  end
end
